// let URL = 'http://ordering.cqmomo.top';
let URL = 'https://orderfood.yundingzhuangyuan.com';
import CryptoJS from 'crypto-js';
import md5 from 'js-md5';
// //#ifndef MP-WEIXIN

// if (process.env.NODE_ENV === 'development') {
// 	console.log('开发环境');
// 	URL = 'https://sales.cqmomo.top/';
// } else {
// 	console.log('生产环境');
// 	URL = 'https://sales.cqmomo.top/';
	
// }
// // #endif
// // #ifdef MP-WEIXIN
// URL = 'http://api.leanplants.com/';
// // #endif
switch (uni.getSystemInfoSync().platform) {
	case 'android':
		console.log('运行Android上')
		break;
	case 'ios':
		console.log('运行iOS上')
		break;
	default:
		console.log('运行在开发者工具上')
		break;
};

const http = (url, data, method) => {
	// let aes_key = '6jqvnrd3ordering';
	// let aes_iv = 'abc123rty456nji7';
	// let key = 'aJeE7zYWFAoJESSS6l8hRkSK5As9Fh2v';
	// console.log(data)
	// let timestamp = parseInt(new Date().getTime() / 1000);
	// let encrypt = CryptoJS.AES.encrypt(JSON.stringify(data), CryptoJS.enc.Utf8.parse(aes_key), {
	//     iv: CryptoJS.enc.Utf8.parse(aes_iv), mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7
	// });
	// let en_data = encrypt.toString();
	// let sign = md5(key + timestamp);
	let token = uni.getStorageSync('token');
	// let token = '934b48035213327eefcdea1ae6c866127fc424a8';
	// let datas = {
	// 	"en_data": en_data,
	// 	"timestamp": timestamp,
	// 	"sign": sign
	// }
	if (token && token != '') {
		data = {
			...data,
			token: token
		}
	};
	uni.showLoading({
		title:'加载中'
	});
	console.log(data);
	return new Promise(function(resolve, reject) {
		uni.request({
			url: URL + url || '',
			method: method || 'POST',
			data: data || null,
			header: {
				"Content-Type": "application/x-www-form-urlencoded", // application/json
				// "Access-Token":uni.getStorageSync('jwt') || '',
				// "Authorization":token||''
			},
			success: function(res) {
				console.log(res)
				uni.hideLoading();
				if(res.data.code==401){
					uni.showToast({
						title:'登录失效',
						icon:'none'
					})
					setTimeout(()=>{
						uni.setStorageSync('token', '');
						uni.reLaunch({
							url: '/pages/tabbar/index'
						})
					},1000)
				}else{
					resolve(res.data);
				}
			},
			fail: function(err) {
				uni.hideLoading();
				console.log(data);
				console.log(JSON.stringify(err));
				reject({
					error: '网络错误'
				});
			},
		})
	})
}

export default http;
